import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api';

import { WeaInputLocale, WeaLocaleProvider } from 'ecCom';

const getCurrentLabel = WeaInputLocale.getCurrentLabel;// 过滤多语言字符串
const getLabel = WeaLocaleProvider.getLabel;// 获取多语言

export default class ApiSingleShowStore {
    @observable apiId = '';// 接口id
    @observable baseList;// 基本信息
    @observable tableList;// 列表
    @observable showTemplet;// 显示模板
    @observable isSuccess = false;// 是否成功数据
    @observable subTableList = [];// 子列表
    @observable apiName = '';// 接口名称
    @observable updata_date = '';// 数据更新时间
    @observable msgcode = '';// 返回信息
    @observable loading = true;// 加载状态
    @observable isList = '0';// 接口内的数据为纯列表
    @observable thisPage = 1;// 纯列表类接口默认显示第一页，启信宝显示前二十条
    @observable total = 1;// 纯列表类接口一共有多少页
    @observable module = '';
    @observable apiJson = '';
    @observable totalNumber = 0;
    @observable skip = 0;
    @observable params = {};
    @observable fuzzyMatchList = false;
    @observable subNameList = [];
    @observable inputSearchValue = '';
    @observable showSpin = true;
    @observable nameLink = '1';// 1为点击企业名称打开单接口；2为分组-单个显示；3为分组显示
    @observable testContain = 'PC';
    @observable comBj = {};
    @observable linkUrl = '';
    @observable title = '';
    @observable lvisible = false;
    @observable columns = [
      {
        dataIndex: 'name',
        isHtml: true,
        key: 'name',
        sortDirections: ['descend', 'ascend'],
        title: getLabel(195, '名称'),
        render: text => (window.navigator.userAgent.match('E-Mobile') ? <span>{text}</span> : <a onClick={() => { this.onNameClick(text); }}>{text}</a>),
      },
      {
        dataIndex: 'id',
        isHtml: true,
        key: 'id',
        sortDirections: ['descend', 'ascend'],
        title: getLabel(714, '编号'),
      },
      {
        dataIndex: 'start_date',
        isHtml: true,
        key: 'start_date',
        sortDirections: ['descend', 'ascend'],
        title: getLabel(500371, '开始时间'),
      },
      {
        dataIndex: 'oper_name',
        isHtml: true,
        key: 'oper_name',
        sortDirections: ['descend', 'ascend'],
        title: getLabel(27843, '法定代表人'),
      },
      {
        dataIndex: 'reg_no',
        isHtml: true,
        key: 'reg_no',
        sortDirections: ['descend', 'ascend'],
        title: getLabel(511139, '注册号'),
      },
      {
        dataIndex: 'credit_no',
        isHtml: true,
        key: 'credit_no',
        sortDirections: ['descend', 'ascend'],
        title: getLabel(511138, '社会统一信用代码'),
      },
    ];

    @action
    init = (comBj) => {
      this.comBj = comBj;
      if (comBj.props.name) {
        this.apiName = comBj.props.name;
      }

      if (comBj.props.showAlertIcon) {
        this.showAlertIcon = comBj.props.showAlertIcon;
      }

      if (comBj.props.params.apiId) {
        this.apiId = comBj.props.params.apiId;
      }

      if (comBj.props.fuzzyMatchList) {
        this.fuzzyMatchList = comBj.props.fuzzyMatchList;
        window.navigator.userAgent.match('E-Mobile') ? this.fuzzyMatchList = false : '';
      }

      if (comBj.props.fuzzyMatchParas) {
        this.EtpFullName = comBj.props.fuzzyMatchParas.EtpFullName;
        this.inputSearchValue = this.EtpFullName;
      }

      if (typeof (comBj.props.showSpin) !== 'undefined') {
        this.showSpin = comBj.props.showSpin;
      }
      if (comBj.props.nameLink) {
        this.nameLink = comBj.props.nameLink;
      }
      this.params = comBj.props.params;
      this.callApi();

      this.fuzzyMatchList && this.getFuzzyMatchList();
    }

    // 调用API
    @action
    callApi = () => {
      Apis.getApi({ ...this.params, isList: 1, skip: this.skip }).then((data) => {
        if (data.status == 'success') {
          // console.log(data);
          if (data.datas.baseList) {
            this.baseList = data.datas.baseList;
          }
          this.apiJson = data.json;
          this.tableList = data.datas.tableList;
          this.subTableList = data.datas.sublist;
          this.updata_date = `${data.updata_date} ${data.updata_time}`;// 最后更新时间
          this.total = Math.ceil(parseInt(data.datas.total) / 5);// 总页数
          this.totalNumber = parseInt(data.datas.total);
          this.isSuccess = true;
        } else {
          this.msgcode = data.msgcode;
          this.isSuccess = false;
        }
        this.loading = false;
      });
    }

    // 清除缓存
    @action
    clrarCatch = () => {
      this.msgcode = '';
      this.updata_date = '';
      this.isSuccess = false;
      this.loading = true;
    }

    @action
    onNameClick = (name) => {
      // console.log(name);
      // console.log(this.inputSearchValue)
      const ua = window.navigator.userAgent;
      const linkType = this.nameLink;
      this.testContain = linkType;
      let linkUrl = `/spa/crm/static/index.html#/main/crm/api/APIShowGroup/crmGroup?keyword=${name}&EtpFullName=${name}`;
      // console.log(linkUrl)
      // this.opDiaLog(linkUrl, name);
      switch (linkType) {
        case '1':
          if (ua.match('E-Mobile')) {
            this.opDiaLog(linkUrl, name);
          } else {
            window.open(linkUrl);
          }
          break;
        case '2':
          linkUrl = `/spa/crm/static/index.html#/main/crm/api/APIShow/crm?keyword=${name}&EtpFullName=${name}`;
          if (ua.match('E-Mobile')) {
            this.opDiaLog(linkUrl, name);
          } else {
            window.open(linkUrl);
          }
          break;
        case '3':
          if (ua.match('E-Mobile')) {
            this.opDiaLog(linkUrl, name);
          } else {
            window.open(linkUrl);
          }
          break;
      }
    }
    @action
    opDiaLog = (linkUrl, name) => {
      this.getDialog();
      this.linkUrl = linkUrl;
      this.title = name;
      setTimeout(() => { this.lvisible = true; }, 1000);
    }
    @action
    closeDia = () => {
      this.lvisible = false;
      this.linkUrl = '';
    }
    @action
    getFuzzyMatchList = () => {
      Apis.getSubNameListForList({ name: this.EtpFullName }).then((result) => {
        const items = [];
        result.data.map((item) => {
          items.push(<a style={{ margin: '0 5px' }} onClick={() => { this.onNameClick(item); }}>{item}</a>);
        });
        this.subNameList = items;
      });
    }
    @action
    onSearchChange = (value) => {
      this.inputSearchValue = value;
    }

    @action
    getDialog = () => {
      this.diaHeight = window.top.document.body.clientHeight - 48;
      this.diaWidth = window.top.document.body.clientWidth;
      // this.diaHeight = document.documentElement.clientHeight - 48;
      // this.diaWidth = document.documentElement.clientWidth;
      // console.log(this.diaHeight);
      // console.log(this.diaWidth);
    }
}
